package com.jrx.anytxn.feign.filter;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @comment: 全局请求流水号实现
 * @author: hsoong
 * @date: 2021/12/4 10:42
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        String value = MDC.get(CustomTraceFilter.LOG_LABEL);
        if (StringUtils.isEmpty(value)) {
            value = UUID.randomUUID().toString();
            MDC.put(CustomTraceFilter.LOG_LABEL, value);
        }

        template.header(CustomTraceFilter.HEADER_REQUEST_ID, value);
    }
}
